{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 day, 0:00:00\n",
      "1:00:00\n",
      "0:30:00\n"
     ]
    }
   ],
   "source": [
    "# 创建一个表示 1 天时间间隔的 timedelta 对象\n",
    "one_day = timedelta(days=1)\n",
    "print(one_day)\n",
    "# 创建一个表示 1 小时时间间隔的 timedelta 对象\n",
    "one_hour = timedelta(hours=1)\n",
    "print(one_hour)\n",
    "# 创建一个表示 30 分钟时间间隔的 timedelta 对象\n",
    "thirty_minutes = timedelta(minutes=30)\n",
    "print(thirty_minutes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-07-08 10:08:23.309056\n",
      "2024-07-09 10:08:23.309056\n",
      "2024-07-07 10:08:23.309056\n"
     ]
    }
   ],
   "source": [
    "# 获取当前日期时间\n",
    "now = datetime.now()\n",
    "print(now)\n",
    "# 计算明天的日期时间\n",
    "tomorrow = now + timedelta(days=1)\n",
    "print(tomorrow)\n",
    "# 计算昨天的日期时间\n",
    "yesterday = now - timedelta(days=1)\n",
    "print(yesterday)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2:30:00\n",
      "2:30:00\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 两个 timedelta 对象相加\n",
    "total_time = timedelta(hours=2) + timedelta(minutes=30)\n",
    "print(total_time)\n",
    "# 两个 timedelta 对象相减\n",
    "difference = timedelta(hours=3) - timedelta(minutes=30)\n",
    "print(difference)\n",
    "# 比较两个 timedelta 对象\n",
    "is_longer = timedelta(hours=2) > timedelta(hours=1)\n",
    "print(is_longer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "9000\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "time_diff = timedelta(days=1, hours=2, minutes=30)\n",
    "print(time_diff.days)        # 输出: 1\n",
    "print(time_diff.seconds)     # 输出: 9000 (2小时30分钟转换为秒)\n",
    "print(time_diff.microseconds)# 输出: 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "end_date= datetime.now()\n",
    "# 计算几天前的日期时间\n",
    "start_date = end_date - timedelta(days=27)\n",
    "\n",
    "start_date = start_date.strftime('%Y%m%d')\n",
    "end_date = end_date.strftime('%Y%m%d')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20240611'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20240708'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start Date: 20240519\n",
      "End Date: 20240708\n"
     ]
    }
   ],
   "source": [
    "def get_date_range(days_before):\n",
    "    end_date = datetime.now()\n",
    "    start_date = end_date - timedelta(days=days_before)\n",
    "    \n",
    "    start_date_str = start_date.strftime('%Y%m%d')\n",
    "    end_date_str = end_date.strftime('%Y%m%d')\n",
    "    \n",
    "    return start_date_str, end_date_str\n",
    "\n",
    "# 示例调用\n",
    "start_date, end_date = get_date_range(50)\n",
    "print(\"Start Date:\", start_date)\n",
    "print(\"End Date:\", end_date)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 每月最后一天日期\n",
    "某个月的最后一天可以通过下个月的第一天减去一天来得到。例如，如果要计算2023年10月的最后一天，可以先计算2023年11月1日，然后减去一天，得到2023年10月31日。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下一个月第一天日期: 2024-03-01\n",
      "2024年2月的最后一天是: 2024-02-29\n"
     ]
    }
   ],
   "source": [
    "from datetime import date, timedelta\n",
    "\n",
    "def last_day_of_month(year, month):\n",
    "    \"\"\"\n",
    "    返回指定年份和月份的最后一天\n",
    "    :param year: int, 年份\n",
    "    :param month: int, 月份\n",
    "    :return: date, 指定年份和月份的最后一天\n",
    "    \"\"\"\n",
    "    # 获取下个月的第一天\n",
    "    if month == 12:\n",
    "        next_month_first_day = date(year + 1, 1, 1)\n",
    "        print('下一个月第一天日期:',date(year + 1, 1, 1))\n",
    "    else:\n",
    "        next_month_first_day = date(year, month + 1, 1)\n",
    "        print('下一个月第一天日期:',date(year, month + 1, 1))\n",
    "    # 下个月的第一天减去一天就是本月的最后一天\n",
    "    return next_month_first_day - timedelta(days=1)\n",
    "\n",
    "# 示例用法\n",
    "year = 2024\n",
    "month = 2\n",
    "last_day = last_day_of_month(year, month)\n",
    "print(f\"{year}年{month}月的最后一天是: {last_day.strftime('%Y-%m-%d')}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-07-08 14:10:31 2024-07-10 14:10:31\n"
     ]
    }
   ],
   "source": [
    "days=2\n",
    "start_time = str(datetime.now() - timedelta(days=days))[:19]\n",
    "end_time = str(datetime.now())[:19]\n",
    "print(start_time, end_time)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
